<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>anchoredviewportposition_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_positioning_anchoredviewportposition_test.js.html">anchoredviewportposition_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.positioning.AnchoredViewportPositionTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.positioning.AnchoredViewportPositionTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line20"></a>goog.require(&#39;goog.positioning.AnchoredViewportPosition&#39;);
<a name="line21"></a>goog.require(&#39;goog.positioning.Corner&#39;);
<a name="line22"></a>goog.require(&#39;goog.positioning.OverflowStatus&#39;);
<a name="line23"></a>goog.require(&#39;goog.style&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>
<a name="line26"></a>var frame, doc, dom, viewportSize, anchor, popup;
<a name="line27"></a>var corner = goog.positioning.Corner;
<a name="line28"></a>
<a name="line29"></a>function setUp() {
<a name="line30"></a>  frame = document.getElementById(&#39;frame1&#39;);
<a name="line31"></a>  doc = goog.dom.getFrameContentDocument(frame);
<a name="line32"></a>  dom = goog.dom.getDomHelper(doc);
<a name="line33"></a>  viewportSize = dom.getViewportSize();
<a name="line34"></a>  anchor = dom.getElement(&#39;anchor&#39;);
<a name="line35"></a>  popup = dom.getElement(&#39;popup&#39;);
<a name="line36"></a>  goog.style.setSize(popup, 20, 20);
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>// The frame has enough space at the bottom of the anchor.
<a name="line40"></a>function testRepositionBottom() {
<a name="line41"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line42"></a>      anchor, corner.BOTTOM_LEFT, false);
<a name="line43"></a>  goog.style.setSize(anchor, 100, 100);
<a name="line44"></a>  goog.style.setPosition(anchor, 0, 0);
<a name="line45"></a>  assertTrue(viewportSize.height &gt;= 100 + 20);
<a name="line46"></a>
<a name="line47"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line48"></a>  var anchorRect = goog.style.getBounds(anchor);
<a name="line49"></a>  assertEquals(anchorRect.top + anchorRect.height,
<a name="line50"></a>               goog.style.getPageOffset(popup).y);
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>// No enough space at the bottom, but at the top.
<a name="line54"></a>function testRepositionTop() {
<a name="line55"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line56"></a>      anchor, corner.BOTTOM_LEFT, false);
<a name="line57"></a>  var newTop = viewportSize.height - 100;
<a name="line58"></a>  goog.style.setSize(anchor, 100, 100);
<a name="line59"></a>  goog.style.setPosition(anchor, 50, newTop);
<a name="line60"></a>  assertTrue(newTop &gt;= 20);
<a name="line61"></a>
<a name="line62"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line63"></a>  anchorRect = goog.style.getBounds(anchor);
<a name="line64"></a>  var popupRect = goog.style.getBounds(popup);
<a name="line65"></a>  assertEquals(anchorRect.top, popupRect.top + popupRect.height);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>// Not enough space either at the bottom or right but there is enough space at
<a name="line69"></a>// top left.
<a name="line70"></a>function testRepositionBottomRight() {
<a name="line71"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line72"></a>      anchor, corner.BOTTOM_RIGHT, false);
<a name="line73"></a>  goog.style.setSize(anchor, 100, 100);
<a name="line74"></a>  goog.style.setPosition(anchor, viewportSize.width - 110,
<a name="line75"></a>      viewportSize.height - 110);
<a name="line76"></a>
<a name="line77"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line78"></a>  anchorRect = goog.style.getBounds(anchor);
<a name="line79"></a>  var popupRect = goog.style.getBounds(popup);
<a name="line80"></a>  assertEquals(anchorRect.top, popupRect.top + popupRect.height);
<a name="line81"></a>  assertEquals(anchorRect.left, popupRect.left + popupRect.width);
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>// Enough space at neither the bottom nor the top.  Adjustment flag is false.
<a name="line85"></a>function testRepositionNoSpaceWithoutAdjustment() {
<a name="line86"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line87"></a>      anchor, corner.BOTTOM_LEFT, false);
<a name="line88"></a>  goog.style.setPosition(anchor, 50, 10);
<a name="line89"></a>  goog.style.setSize(anchor, 100, viewportSize.height - 20);
<a name="line90"></a>
<a name="line91"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line92"></a>  anchorRect = goog.style.getBounds(anchor);
<a name="line93"></a>  popupRect = goog.style.getBounds(popup);
<a name="line94"></a>  assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
<a name="line95"></a>  assertTrue(popupRect.top + popupRect.height &gt; viewportSize.height);
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>// Enough space at neither the bottom nor the top.  Adjustment flag is true.
<a name="line99"></a>function testRepositionNoSpaceWithAdjustment() {
<a name="line100"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line101"></a>      anchor, corner.BOTTOM_LEFT, true);
<a name="line102"></a>  goog.style.setPosition(anchor, 50, 10);
<a name="line103"></a>  goog.style.setSize(anchor, 100, viewportSize.height - 20);
<a name="line104"></a>
<a name="line105"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line106"></a>  anchorRect = goog.style.getBounds(anchor);
<a name="line107"></a>  popupRect = goog.style.getBounds(popup);
<a name="line108"></a>  assertTrue(anchorRect.top + anchorRect.height &gt; popupRect.top);
<a name="line109"></a>  assertEquals(viewportSize.height, popupRect.top + popupRect.height);
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>function testAdjustCorner() {
<a name="line113"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line114"></a>      anchor, corner.BOTTOM_LEFT);
<a name="line115"></a>  assertEquals(corner.BOTTOM_LEFT, avp.adjustCorner(0, corner.BOTTOM_LEFT));
<a name="line116"></a>  assertEquals(corner.BOTTOM_RIGHT, avp.adjustCorner(
<a name="line117"></a>      goog.positioning.OverflowStatus.FAILED_HORIZONTAL, corner.BOTTOM_LEFT));
<a name="line118"></a>  assertEquals(corner.TOP_LEFT, avp.adjustCorner(
<a name="line119"></a>      goog.positioning.OverflowStatus.FAILED_VERTICAL, corner.BOTTOM_LEFT));
<a name="line120"></a>  assertEquals(corner.TOP_RIGHT, avp.adjustCorner(
<a name="line121"></a>      goog.positioning.OverflowStatus.FAILED_VERTICAL |
<a name="line122"></a>      goog.positioning.OverflowStatus.FAILED_HORIZONTAL,
<a name="line123"></a>      corner.BOTTOM_LEFT));
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>// No space to fit, so uses fallback.
<a name="line127"></a>function testOverflowConstraint() {
<a name="line128"></a>  var tinyBox = new goog.math.Box(0, 0, 0, 0);
<a name="line129"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line130"></a>      anchor, corner.BOTTOM_LEFT, false, tinyBox);
<a name="line131"></a>  assertEquals(tinyBox, avp.getOverflowConstraint());
<a name="line132"></a>
<a name="line133"></a>  goog.style.setSize(anchor, 50, 50);
<a name="line134"></a>  goog.style.setPosition(anchor, 80, 80);
<a name="line135"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line136"></a>
<a name="line137"></a>  anchorRect = goog.style.getBounds(anchor);
<a name="line138"></a>  popupRect = goog.style.getBounds(popup);
<a name="line139"></a>  assertEquals(anchorRect.left, popupRect.left);
<a name="line140"></a>  assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>// Initially no space to fit above, then changes to have room.
<a name="line144"></a>function testChangeOverflowConstraint() {
<a name="line145"></a>  var tinyBox = new goog.math.Box(0, 0, 0, 0);
<a name="line146"></a>  var avp = new goog.positioning.AnchoredViewportPosition(
<a name="line147"></a>      anchor, corner.BOTTOM_LEFT, false, tinyBox);
<a name="line148"></a>  assertEquals(tinyBox, avp.getOverflowConstraint());
<a name="line149"></a>
<a name="line150"></a>  goog.style.setSize(anchor, 50, 50);
<a name="line151"></a>  goog.style.setPosition(anchor, 80, 80);
<a name="line152"></a>
<a name="line153"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line154"></a>  popupRect = goog.style.getBounds(popup);
<a name="line155"></a>  assertNotEquals(60, popupRect.top);
<a name="line156"></a>
<a name="line157"></a>  var movedBox = new goog.math.Box(60, 100, 100, 60);
<a name="line158"></a>  avp.setOverflowConstraint(movedBox);
<a name="line159"></a>  assertEquals(movedBox, avp.getOverflowConstraint());
<a name="line160"></a>
<a name="line161"></a>  avp.reposition(popup, corner.TOP_LEFT);
<a name="line162"></a>  popupRect = goog.style.getBounds(popup);
<a name="line163"></a>  assertEquals(80, popupRect.left);
<a name="line164"></a>  assertEquals(60, popupRect.top);
<a name="line165"></a>}
</pre>


</body>
</html>
